﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    public class DownloadController : Controller
    {
        private int PageSize = 3;
        //
        // GET: /Download/
        
        private IProductRepository productRepository;

        public DownloadController(IProductRepository proRepo)
        {
            productRepository = proRepo;
        }

        private IEnumerable<string> GetProductCategory() //for those product having documents, get their category
        {
            return
                productRepository.Products.Where(p => p.Documents.Count > 0)
                                 .Select(p => p.Category.Name)
                                 .Distinct()
                                 .OrderBy(c=>c);
        }

        

        public ViewResult List(string category, int page =1)
        {
            IEnumerable <Product> productsWithDocuments = productRepository.Products.Where(p=>p.Documents.Count>0).ToList();
            DownloadListViewModel viewModel = new DownloadListViewModel
                {
                    Products = productsWithDocuments.Where(p=>p.Category.Name==category || category==null).OrderBy(p=>p.Id).Skip((page-1)*PageSize).Take(PageSize),
                    PagingInfo = new PagingInfo
                        {
                            CurrentPage = page,
                            ItemsPerPage = PageSize,
                            //TotalItems = category==null?productRepository.Products.Count():productRepository.Products.Count(p=>p.Category==category),
                            TotalItems = category==null?productsWithDocuments.Count():productsWithDocuments.Count(p=>p.Category.Name==category)
                        },
                    CurrentCategory = category,
                    Categories = GetProductCategory(),
                    BreadCrumb = category == null ? new Dictionary<string, string> { { "公司首页", "/" }, { "资料下载", "" } } : new Dictionary<string, string> { { "公司首页", "/" }, { "资料下载", "/Download/List" },{category,""} },

                };
            ViewBag.SelectedCategory = category;
            return View(viewModel);
        }

    }
}
